import 'package:flutter/material.dart';

/// 添加或移除组件
class AddRemoveApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Add Remove App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AddRemovePage(),
    );
  }
}

/// 页面
class AddRemovePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _AddRemovePageState();
}

/// 页面状态
class _AddRemovePageState extends State<AddRemovePage> {
  // 默认显示
  bool toggle = true;

  /// 设置是否显示
  void _toggle() {
    setState(() {
      toggle = !toggle;
    });
  }

  /// 获取显示控件
  _getToggleChild() {
    if (toggle) {
      return Text('Toggle One');
    } else {
      return MaterialButton(
        onPressed: () {},
        child: Text('Toggle Two'),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sample App'),
      ),
      body: Center(
        child: _getToggleChild(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _toggle,
        tooltip: 'Update Text',
        child: Icon(Icons.update),
      ),
    );
  }
}
